﻿using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using IPMS.Sort;
using IPMS.Common;
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using System.Web;
using SysOperationLayer.DBHandle;
using System.Data;
using SysComm;

public partial class SysUsers : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    private void getUsersList(string strLoginName, string strUserName, string strGroupName, string strSuspend, int nUserRole)
    {
        UsersDB objDB = new UsersDB();
        DataTable dt = objDB.GetInfo(strLoginName, strUserName, strGroupName, strSuspend, nUserRole);
        gvUserList.DataSource = dt;
        gvUserList.DataBind();
        objDB.CloseDB();
    }

    private void getUsersList()
    {
        getUsersList(txtSearchLoginName.Text.Trim(), txtSearchUserName.Text.Trim(), txtSearchGroupName.Text.Trim(), lstSuspend.SelectedValue, lstUserRole.SelectedIndex);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorUsertype())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else if (!IsPostBack)
        {
            ((Label)this.Master.FindControl("lblMstTitle")).Text = "| " + ISitemap.Settings + "|" + ISitemap.UserSettings.ToString() + "| " + ISitemap.UserManagement.ToString();
            lstUserRole.Items.Add("*");
            lstUserRole.Items.Add("Management");
            lstUserRole.Items.Add("Supervisor");
            lstUserRole.Items.Add("User");
            lstUserRole.Items.Add("Engineer");
            lstUserRole.SelectedIndex = 0;

            lstSuspend.Items.Add("*");
            lstSuspend.Items.Add("Y");
            lstSuspend.Items.Add("N");
            lstSuspend.SelectedIndex = 0;

            getUsersList();
        }
    }

    protected void OnClick(object sender, EventArgs e)
    {
        if (sender == btnSearch)
        {
            getUsersList();
        }
        else if (sender == btnNew)
        {
            Response.Redirect("SysUsersAdd.aspx");
        }
    }

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btnTrash = (ImageButton)e.Row.FindControl("btnTrash");
            btnTrash.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");
        }
    }

    protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow curRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
        string strRowno = gvUserList.DataKeys[curRow.RowIndex].Values["Rowno"].ToString();

        if (e.CommandName == "Edit")
        {
            Response.Redirect("SysUsersUpdate.aspx?userid=" + strRowno);
        }
        else if (e.CommandName == "ResetPassword")
        {
            Response.Redirect("SysResetPasswd.aspx?userid=" + strRowno);
        }
        else if (e.CommandName == "Trash")
        {
            UsersDB objDB = new UsersDB();

            if (objDB.Trash(strRowno) == 0)
            {
                new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_user_manage, "Delete User Account Success!");
                objDB.CloseDB();
                Jscript.Alert("Delete User Account Success!", this.Page);
                getUsersList();
            }
            else
            {
                new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_user_manage, "Delete User Account Failure!");
                objDB.CloseDB();
                Jscript.Alert("Delete User Account Failure!", this.Page);
            }
        }
    }
}
